<?php
/**
 *
 * Helper to generate a series of form fields for date/time input.
 * 
 * @category Abovo
 * 
 * @package Abovo_View
 * 
 * @author Rodrigo Moraes <rodrigo.moraes@gmail.com>
 * 
 * @author Antti Holvikari <anttih@gmail.com>
 * 
 * @license http://opensource.org/licenses/bsd-license.php BSD
 * 
 * @version $Id$
 * 
 */

/**
 *
 * Helper to generate a series of form fields for date/time input.
 *
 * @category Abovo
 *
 * @package Abovo_View
 *
 */
class Abovo_View_Helper_FormDate extends Solar_View_Helper_FormElement
{
    /**
     *
     * User-provided configuration values. Keys are...
     * 
     * `config`
     * : (array) Array of default values to be used on each helper call. Keys:
     * 
     * `day`
     * : (string) Name of the 'day' input field.
     * 
     * `month`
     * : (string) Name of the 'month' input field.
     * 
     * `year`
     * : (string) Name of the 'year' input field.
     * 
     * @var array
     * 
     */
    protected $_Abovo_View_Helper_FormDate = array(
        'config' => array(
            'day'    => 'day',
            'month'  => 'month',
            'year'   => 'year',
            'hour'   => 'hour',
            'minute' => 'minute',
        ),
    );
    
    /**
     * 
     * Returns a series of form fields for date/time input.
     * 
     * @param string $spec Any date-time string suitable for strtotime().
     * 
     * @param array $config Form field name definitions to override the default
     * ones.
     * 
     * @return string The form group.
     * 
     */
    public function formDate($info)
    {
        $this->_prepare($info);
        
        if(! $this->_value) {
            $time = time();
        } else {
            $time = strtotime($this->_value);
        }
        
        $config = array_merge($this->_config['config'], $this->_options);
        
        $form = array();
        
        // Day.
        $form[] = $this->_view->formText(
            array(
                'name'    => $config['day'],
                'value'   => date('d', $time),
                'attribs' => array(
                    'size'      => 2,
                    'maxlength' => 2,
                ),
            )
        );
        
        // Month.
        $form[] = '/' . $this->_view->formText(
            array(
                'name'    => $config['month'],
                'value'   => date('m', $time),
                'attribs' => array(
                    'size'      => 2,
                    'maxlength' => 2,
                ),
            )
        );
        
        // Year.
        $form[] = '/' . $this->_view->formText(
            array(
                'name'    => $config['year'],
                'value'   => date('Y', $time),
                'attribs' => array(
                    'size'      => 4,
                    'maxlength' => 4,
                ),
            )
        );
        
        // Return the form.
        return implode('', $form);
    }
}